import Vue from "vue";
import Router from "vue-router";
import { message } from "ant-design-vue";
Vue.use(Router);
import { getToken } from "@/util/token";
import index from "@/layout/index.vue";
// 无权限路由


import Singer from "@/views/Singer/index"
import SingerDetail from "@/views/singerDetail/index"
import Album from "@/views/Album/index";
import MusicListDetail from '@/views/MusicListDetail/index'
import Upload from "@/views/Download/index"
import MuiscListRecommend from "@/views/MuiscListRecommend/index";

export const constantRoutes = [
         {
           name: "upload",
           path: "/upload",
           component: Upload
         },
         {
           name: "index",
           path: "/",
           component: index,
           redirect: "/index",
           children: [
             {
               path: "/index",
               component: () => import("@/views/Home/index.vue")
             },
             {
               name: "MuiscListRecommend",
               path: "/muisclistrecommend",
               component: MuiscListRecommend
             },
             {
               name: "User",
               path: "/user",
               redirect: "user/home",
               component: () => import("@/views/User/index.vue"),
               children: [
                 {
                   name: "UserHome",
                   path: "/user/home",
                   component: () => import("@/views/UserHome/index.vue")
                 },
                 {
                   name: "Update",
                   path: "/user/update",
                   component: () => import("@/views/UserUpdate/index.vue")
                 }
               ]
             },
             {
               name: "Singer",
               path: "/singer",
               component: Singer
             },
             {
               name: "SingerDetail",
               path: "/singerdetail/:id",
               component: SingerDetail
             },
             {
               name: "Album",
               path: "/album/:id",
               component: Album
             },
             {
               name: "MusicListDetail",
               path: "/musiclistdetail/:id",
               component: MusicListDetail
             }
           ]
         },
         {
           name: "ErrorPages",
           path: "/404",
           component: () => import("@/views/error-page/404.vue")
         },
         {
           name: "login",
           path: "/login",
           component: () => import("@/views/Login/index.vue")
         },

         {
           path: "*",
           redirect: "/404",
           hidden: true
         }
       ];

// 有权限路由
export const asyncRoutes = [
  // 404页面需要放在最后
];

const createRouter = () =>
  new Router({
    scrollBehavior: () => ({
      y: 0
    }),
    routes: constantRoutes
  });

const router = createRouter();

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

// 全局前置守卫
router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
    next();
  } else {
    let token = getToken();
    if (token === undefined || token === "") {
      message.error("身份已过期，请重新登录");
      next("/login");
    } else {
      next();
    }
  }
});

// 全局后置守卫
// router.afterEach((to,from)=>{

// })

export default router;
